package com.bjb.vr.operation.service;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.bjb.vr.common.ao.OptCusDevCtrlAo;
import com.bjb.vr.common.ao.ResDeviceCtrlAo;
import com.bjb.vr.common.constant.ThreadLocalConstant;
import com.bjb.vr.common.dto.ResGroup;
import com.bjb.vr.common.exception.CommonException;
import com.bjb.vr.common.mapper.ResGroupMapper;
import com.bjb.vr.common.utils.ThreadLocalUtil;
import com.bjb.vr.operation.rabbitmq.*;
import com.bjb.vr.operation.utils.MQUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * @version 1.0.0
 * @description:设备控制类
 * @author: caihaibo
 * @createDate: 2022/9/21
 */
@Service
public class ResDeviceCtrlService {

	@Autowired
	ResGroupMapper resGroupMapper;


	public void sendRestartCommand(String tenantId,String[] udids){
		ThreadLocalUtil.set(ThreadLocalConstant.NO_TENANT_ID_VERIFY, true);

		LambdaQueryWrapper<ResGroup> lambdaQueryWrapper = new LambdaQueryWrapper<>();
		lambdaQueryWrapper.eq(ResGroup::getTenantId, tenantId);
		List<ResGroup> resGroupList = resGroupMapper.selectList(lambdaQueryWrapper);


		if (resGroupList == null || resGroupList.size() == 0) {
			throw new CommonException("该租户下没有设备分组");
		}
		if (resGroupList.size() > 1) {
			throw new CommonException("该租户下存在多个设备分组");
		}
		ResGroup resGroup = resGroupList.get(0);
		if (StringUtils.isEmpty(resGroup.getGroupCode())) {
			throw new CommonException("设备分组编码不能为空");
		}
		String groupCode = resGroup.getGroupCode();

		for(String udid:udids) {
			RabbitWebSocketMsg rwsc = new RabbitWebSocketMsg();
			rwsc.setGroupCode(groupCode);
			rwsc.setDeviceId(groupCode);
			rwsc.setIsAdmin("1");
			rwsc.setTenantId(tenantId);
			rwsc.setMsgType("TEXT");
			String msg = getJsonMsg(udid,"vrUpdateAuthority");
			rwsc.setMsg(msg);
			String json = JSONObject.toJSONString(rwsc);
			MQUtil.send("fanout.websocket.exchange", "", json);

		}
		ThreadLocalUtil.set(ThreadLocalConstant.NO_TENANT_ID_VERIFY, false);
	}

	public void sendRestartCommand(String tenantId){
		ThreadLocalUtil.set(ThreadLocalConstant.NO_TENANT_ID_VERIFY, true);

		LambdaQueryWrapper<ResGroup> lambdaQueryWrapper = new LambdaQueryWrapper<>();
		lambdaQueryWrapper.eq(ResGroup::getTenantId, tenantId);
		List<ResGroup> resGroupList = resGroupMapper.selectList(lambdaQueryWrapper);


		if (resGroupList == null || resGroupList.size() == 0) {
			throw new CommonException("该租户下没有设备分组");
		}
		if (resGroupList.size() > 1) {
			throw new CommonException("该租户下存在多个设备分组");
		}
		ResGroup resGroup = resGroupList.get(0);
		if (StringUtils.isEmpty(resGroup.getGroupCode())) {
			throw new CommonException("设备分组编码不能为空");
		}
		String groupCode = resGroup.getGroupCode();

		RabbitWebSocketMsg rwsc = new RabbitWebSocketMsg();
		rwsc.setGroupCode(groupCode);
		rwsc.setDeviceId(groupCode);
		rwsc.setIsAdmin("1");
		rwsc.setTenantId(tenantId);
		rwsc.setMsgType("TEXT");
		String msg = getJsonMsg("vrUpdateAuthority");
		rwsc.setMsg(msg);
		String json = JSONObject.toJSONString(rwsc);
		MQUtil.send("fanout.websocket.exchange", "", json);

		ThreadLocalUtil.set(ThreadLocalConstant.NO_TENANT_ID_VERIFY, false);
	}

	public void devBatchCtrl(OptCusDevCtrlAo optCusDevCtrlAo) {
		Assert.notNull(optCusDevCtrlAo.getResDeviceCtrlAos(), "请选择操作的设备！");
		Assert.notNull(optCusDevCtrlAo.getCommand(), "操作命令不能为空！");

		if(optCusDevCtrlAo.getResDeviceCtrlAos().length <=0){
			throw new CommonException("请选择操作的设备！");
		}
		ThreadLocalUtil.set(ThreadLocalConstant.NO_TENANT_ID_VERIFY, true);

		for(ResDeviceCtrlAo resDeviceCtrlAo:optCusDevCtrlAo.getResDeviceCtrlAos()) {
			Assert.notNull(resDeviceCtrlAo.getTenantId(), resDeviceCtrlAo.getUdid() + ",租户ID不能为空！");
			LambdaQueryWrapper<ResGroup> lambdaQueryWrapper = new LambdaQueryWrapper<>();
			lambdaQueryWrapper.eq(ResGroup::getTenantId, resDeviceCtrlAo.getTenantId());
			List<ResGroup> resGroupList = resGroupMapper.selectList(lambdaQueryWrapper);


			if (resGroupList == null || resGroupList.size() == 0) {
				throw new CommonException("该租户下没有设备分组");
			}
			if (resGroupList.size() > 1) {
				throw new CommonException("该租户下存在多个设备分组");
			}
			ResGroup resGroup = resGroupList.get(0);
			if (StringUtils.isEmpty(resGroup.getGroupCode())) {
				throw new CommonException("设备分组编码不能为空");
			}
			String groupCode = resGroup.getGroupCode();

			RabbitWebSocketMsg rwsc = new RabbitWebSocketMsg();
			rwsc.setGroupCode(groupCode);
			rwsc.setDeviceId(groupCode);
			rwsc.setIsAdmin("1");
			rwsc.setTenantId(resDeviceCtrlAo.getTenantId());
			rwsc.setMsgType("TEXT");
			String msg = getJsonMsg(resDeviceCtrlAo.getUdid(), optCusDevCtrlAo.getCommand());
			rwsc.setMsg(msg);
			String json = JSONObject.toJSONString(rwsc);
			MQUtil.send("fanout.websocket.exchange", "", json);
		}

		ThreadLocalUtil.set(ThreadLocalConstant.NO_TENANT_ID_VERIFY, false);

	}

	public void devSingleCtrl(OptCusDevCtrlAo optCusDevCtrlAo) {
		Assert.notNull(optCusDevCtrlAo.getTenantId(), "租户id不能为空！");
		Assert.notNull(optCusDevCtrlAo.getUdid(), "请选择操作的设备！");
		Assert.notNull(optCusDevCtrlAo.getCommand(), "操作命令不能为空！");

		ThreadLocalUtil.set(ThreadLocalConstant.NO_TENANT_ID_VERIFY, true);

		LambdaQueryWrapper<ResGroup> lambdaQueryWrapper = new LambdaQueryWrapper<>();
		lambdaQueryWrapper.eq(ResGroup::getTenantId,optCusDevCtrlAo.getTenantId());
		List<ResGroup> resGroupList =  resGroupMapper.selectList(lambdaQueryWrapper);

		ThreadLocalUtil.set(ThreadLocalConstant.NO_TENANT_ID_VERIFY, false);

		if(resGroupList == null || resGroupList.size() ==0){
			throw new CommonException("该租户下没有设备分组");
		}
		if(resGroupList.size()>1){
			throw new CommonException("该租户下存在多个设备分组");
		}
		ResGroup resGroup = resGroupList.get(0);
		if(StringUtils.isEmpty(resGroup.getGroupCode())){
			throw new CommonException("设备分组编码不能为空");
		}
		String groupCode = resGroup.getGroupCode();

		RabbitWebSocketMsg rwsc = new RabbitWebSocketMsg();
		rwsc.setGroupCode(groupCode);
		rwsc.setDeviceId(groupCode);
		rwsc.setIsAdmin("1");
		rwsc.setTenantId(optCusDevCtrlAo.getTenantId());
		rwsc.setMsgType("TEXT");
		String msg = "";
		if(optCusDevCtrlAo.getCommand().equals("vrSwitchSingle")) {
			msg = getModeJsonMsg(optCusDevCtrlAo.getUdid(), optCusDevCtrlAo.getCommand(),optCusDevCtrlAo.getMode());
		}else{
			msg = getJsonMsg(optCusDevCtrlAo.getUdid(), optCusDevCtrlAo.getCommand());
		}

		rwsc.setMsg(msg);
		String json = JSONObject.toJSONString(rwsc);
		MQUtil.send("fanout.websocket.exchange","", json);

	}


	private String getJsonMsg(String[] udids,String command){

		Action action = new Action();
		Command cmd = new Command();

		BatchDevCtrlParameter parameter = new BatchDevCtrlParameter();
		parameter.setDevUdids(udids);
		action.setAction(command);
		cmd.setParameter(parameter);
		action.setCommand(cmd);

		return JSONObject.toJSONString(action);
	}

	private String getJsonMsg(String udid,String command){

		Action action = new Action();
		Command cmd = new Command();
		cmd.setUdid(udid);
		if(command.equals("vrSwitchSingle")){
			SingleModeParameter parameter = new SingleModeParameter();
		}
		action.setAction(command);
		action.setCommand(cmd);

		return JSONObject.toJSONString(action);
	}

	private String getJsonMsg(String command){

		Action action = new Action();
		Command cmd = new Command();
		action.setAction(command);
		action.setCommand(cmd);

		return JSONObject.toJSONString(action);
	}


	private String getModeJsonMsg(String udid,String command,Integer mode ){

		Action action = new Action();
		Command cmd = new Command();
		cmd.setUdid(udid);
		if(command.equals("vrSwitchSingle")){
			SingleModeParameter parameter = new SingleModeParameter();
			parameter.setSingleMode(mode);
			cmd.setParameter(parameter);
		}
		action.setAction(command);
		action.setCommand(cmd);

		return JSONObject.toJSONString(action);
	}
}
